package com.yanqu.road.server.gameplayer.module.activity;

import com.yanqu.road.dao.impl.player.UserActivityWorshipDaoImpl;
import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.activity.zhenhuan.UserActivityWorshipData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


//全程需要触发活动条件公共类
public class ActivityWorshipModule extends GeneralModule {
    public ActivityWorshipModule(GamePlayer player) {
        super(player);
    }

    private Map<Integer, UserActivityWorshipData> worshipDataMap = new ConcurrentHashMap<>();

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        List<UserActivityWorshipData> list = getUserActivityWorshipDataList();
        for (UserActivityWorshipData data : list) {
            if (data.isInsertOption()) {
                new UserActivityWorshipDaoImpl().add(data);
            } else if (data.isUpdateOption()) {
                new UserActivityWorshipDaoImpl().update(data);
            }
        }
        return true;
    }

    @Override
    public boolean loadData() {
        Map<Integer, UserActivityWorshipData> tempMap = new ConcurrentHashMap<>();
        List<UserActivityWorshipData> tempList = new UserActivityWorshipDaoImpl().getUserActivityWorshipData(getUserId());
        for (UserActivityWorshipData userActivityWorshipData : tempList) {
            ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(userActivityWorshipData.getActivityId());
            if (openActivityInfo == null) {
                continue;
            }

            tempMap.put(userActivityWorshipData.getActivityId(), userActivityWorshipData);
        }
        worshipDataMap = tempMap;
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserData();
    }

    public void syncUserData() {
        CommonActivityProto.CommonActivityWorshipUserSyncMsg.Builder syncMsg = CommonActivityProto.CommonActivityWorshipUserSyncMsg.newBuilder();
        for (UserActivityWorshipData worshipData : worshipDataMap.values()) {
            CommonActivityProto.CommonActivityWorshipTemp.Builder temp = CommonActivityProto.CommonActivityWorshipTemp.newBuilder();
            temp.setActivityId(worshipData.getActivityId());
            temp.setWorship(worshipData.getIsWorship());
            syncMsg.addWorshipList(temp);
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_WORSHIP_USER_SYNC, syncMsg);
    }

    public List<UserActivityWorshipData> getUserActivityWorshipDataList() {
        synchronized (worshipDataMap) {
            return new ArrayList<>(worshipDataMap.values());
        }
    }

    public int worship(int activityId) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityId);
        ActivityConfig worshipConfig = activityConfigMap.get("WORSHIP_REWARD");
        if (worshipConfig == null) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (!ActivityMgr.activityInRewardPeriod(openActivityInfo)) {
            return GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME;
        }

        Property reward = PropertyHelper.parseStringToProperty(worshipConfig.getValue());
        if (reward.isNothing()) {
            return GameErrorCode.E_ERROR_DATA;
        }

        UserActivityWorshipData worshipData = worshipDataMap.get(activityId);
        if (worshipData == null) {
            synchronized (worshipDataMap) {
                worshipData = worshipDataMap.get(activityId);
                if (worshipData == null) {
                    worshipData = new UserActivityWorshipData(activityId, getUserId());
                    worshipDataMap.put(activityId, worshipData);
                }
            }
        }

        if (worshipData.getIsWorship() == 1) {
            return GameErrorCode.E_ZHENHUAN_WORSHIP_REPEAT;
        }

        worshipData.setIsWorship(1);
        worshipData.setWorshipTime(DateHelper.getCurrentTime());

        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.ActivityWorship);

        ZhenHuanProto.ZhenHuanWorshipRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanWorshipRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(activityId);
        respMsg.setReward(worshipConfig.getValue());
        player.sendPacket(ClientProtocol.U_ZHENHUAN_WORSHIP, respMsg);

        syncUserData();
        return 0;
    }

    public boolean isWorship(int activityId) {
        UserActivityWorshipData worshipData = worshipDataMap.get(activityId);
        if (worshipData == null) {
            return false;
        }
        return worshipData.getIsWorship() == 1;
    }
}
